#include "relay.h"

relay_config_t relays[3] = {
    {16, 1, "继电器1"}, // GPIO16，高电平触发
    {17, 0, "继电器2"} , // GPIO17，低电平触发
    {20, 0, "继电器3"}  // GPIO20，低电平触发
};

// 初始化所有继电器GPIO
void relays_init() {
    for (int i = 0; i < NUM_RELAYS; i++) {
        gpio_config_t io_conf = {
            .pin_bit_mask = (1ULL << relays[i].gpio_num),
            .mode = GPIO_MODE_OUTPUT,
            .pull_up_en = GPIO_PULLUP_DISABLE,
            .pull_down_en = GPIO_PULLDOWN_DISABLE,
            .intr_type = GPIO_INTR_DISABLE
        };
        gpio_config(&io_conf);
        
        // 初始状态设为关闭
        //gpio_set_level(relays[i].gpio_num, relays[i].trigger_type ? 0 : 1);
    }
    relay_control(0,0);
    relay_control(1,1);
    relay_control(2,1);
}

// 控制指定继电器
void relay_control(int relay_num, bool on) {
    if (relay_num < 0 || relay_num >= NUM_RELAYS) return;
    
    int level;
    if (relays[relay_num].trigger_type) {
        level = on ? 1 : 0; // 高电平触发
    } else {
        level = on ? 0 : 1; // 低电平触发
    }
    
    gpio_set_level(relays[relay_num].gpio_num, level);

}
